<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"mixley.github.io","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="https:&#x2F;&#x2F;www.cnblogs.com&#x2F;javastack&#x2F;p&#x2F;13552577.html 吊打 Tomcat ，Undertow 性能很炸！！ 在 Java Web 容器的世界里，Tomcat 和 Jetty 是大名鼎鼎的、用的最多的开源项目，也是大众熟知的。 今天再介绍另外一款能和 Tomcat 媲美的神器：Undertow，据说性能方面还要吊打 Tomcat，如果你还不知道它，那你就">
<meta property="og:type" content="article">
<meta property="og:title" content="Undertow">
<meta property="og:url" content="https://mixley.github.io/2022/3FH16VE.html">
<meta property="og:site_name" content="mixley">
<meta property="og:description" content="https:&#x2F;&#x2F;www.cnblogs.com&#x2F;javastack&#x2F;p&#x2F;13552577.html 吊打 Tomcat ，Undertow 性能很炸！！ 在 Java Web 容器的世界里，Tomcat 和 Jetty 是大名鼎鼎的、用的最多的开源项目，也是大众熟知的。 今天再介绍另外一款能和 Tomcat 媲美的神器：Undertow，据说性能方面还要吊打 Tomcat，如果你还不知道它，那你就">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://mixley.github.io/2022/3FH16VE/1218593-20200824105305699-195814623.png">
<meta property="og:image" content="https://mixley.github.io/2022/3FH16VE/1218593-20200824105306098-1526118676.png">
<meta property="og:image" content="https://mixley.github.io/2022/3FH16VE/1218593-20200824105306581-130064430.png">
<meta property="og:image" content="https://mixley.github.io/2022/3FH16VE/1218593-20200824105306889-830058720.png">
<meta property="article:published_time" content="2022-06-12T06:35:17.374Z">
<meta property="article:modified_time" content="2022-04-22T07:31:52.000Z">
<meta property="article:author" content="mixley">
<meta property="article:tag" content="java">
<meta property="article:tag" content="spring">
<meta property="article:tag" content="spring boot">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://mixley.github.io/2022/3FH16VE/1218593-20200824105305699-195814623.png">

<link rel="canonical" href="https://mixley.github.io/2022/3FH16VE.html">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Undertow | mixley</title>
  


  <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?c36eb18366b436984557126f16796f30";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">mixley</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">求上者得中·求中者得下</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-sitemap">

    <a href="/sitemap.xml" rel="section"><i class="fa fa-sitemap fa-fw"></i>站点地图</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://mixley.github.io/2022/3FH16VE.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="mixley">
      <meta itemprop="description" content="全栈·架构设计·算法">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="mixley">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Undertow
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2022-06-12 14:35:17" itemprop="dateCreated datePublished" datetime="2022-06-12T14:35:17+08:00">2022-06-12</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2022-04-22 15:31:52" itemprop="dateModified" datetime="2022-04-22T15:31:52+08:00">2022-04-22</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/learn/" itemprop="url" rel="index"><span itemprop="name">学习</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/learn/java/" itemprop="url" rel="index"><span itemprop="name">java</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Changyan：</span>
    
    
      <a title="changyan" href="/2022/3FH16VE.html#SOHUCS" itemprop="discussionUrl">
        <span id="changyan_count_unit" class="post-comments-count hc-comment-count" data-xid="2022/3FH16VE.html" itemprop="commentCount"></span>
      </a>
    
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p><a target="_blank" rel="noopener" href="https://www.cnblogs.com/javastack/p/13552577.html">https://www.cnblogs.com/javastack/p/13552577.html</a></p>
<h1 id="吊打-Tomcat-，Undertow-性能很炸！！"><a href="#吊打-Tomcat-，Undertow-性能很炸！！" class="headerlink" title="吊打 Tomcat ，Undertow 性能很炸！！ "></a><a target="_blank" rel="noopener" href="https://www.cnblogs.com/javastack/p/13552577.html">吊打 Tomcat ，Undertow 性能很炸！！ </a></h1><p>在 Java Web 容器的世界里，Tomcat 和 Jetty 是大名鼎鼎的、用的最多的开源项目，也是大众熟知的。</p>
<p>今天再介绍另外一款能和 Tomcat 媲美的神器：Undertow，据说性能方面还要吊打 Tomcat，如果你还不知道它，那你就 OUT 了。</p>
<p>我们来看下 Spring Boot 默认支持的三种 Servlet 容器：</p>
<table>
<thead>
<tr>
<th>Name</th>
<th>Servlet Version</th>
</tr>
</thead>
<tbody><tr>
<td>Tomcat 9.0</td>
<td>4.0</td>
</tr>
<tr>
<td>Jetty 9.4</td>
<td>3.1</td>
</tr>
<tr>
<td>Undertow 2.0</td>
<td>4.0</td>
</tr>
</tbody></table>
<p>以上来源于 Spring Boot 2.3.2 官方文档，更多信息请点击<a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/YfPFl1_l2sp9YirTppzRow">这里</a>参考这篇文章。</p>
<p>Undertow 它能成为 Spring Boot 默认集成的三大容器之一，就凭这点，我想就足以说明它的地位。</p>
<h2 id="Undertow-什么鬼？"><a href="#Undertow-什么鬼？" class="headerlink" title="Undertow 什么鬼？"></a>Undertow 什么鬼？</h2><p>Undertow 是 RedHat(红帽公司)的开源产品，采用 Java 开发，是一款灵活、高性能的 Web 服务器，提供了基于 NIO 的阻塞/非阻塞 APIs，也是 Wildfly 的默认 Web 容器。</p>
<p>搜索 Undertow：</p>
<p><img src="/2022/3FH16VE/1218593-20200824105305699-195814623.png" alt="img"></p>
<p>页面显示的是 JBoss Community，因为 2006 年 RedHat 收购了 JBoss，那也就不足为怪了。</p>
<p>Undertow 它是一个基于组合的体系结构，可以通过组合一系列小型处理器来构建一个 Web 服务器。这就让我们可以灵活的在 Java EE servlet 4.0 容器和底层非阻塞处理器或者其他更多之间进行选择。</p>
<p>Undertow 被设计成完全可嵌入式的，所以也叫嵌入式容器，具有易于使用的流畅构建 API，另外，Undertow 的生命周期也完全由所嵌入的应用程序所控制。</p>
<p>这也是为什么 Spring Boot 可以直接嵌入 Undertow 的原因，Undertow 它就是为了嵌入而发烧的。Spring Boot 基础知识就不介绍了，关注公众号Java技术栈在后台回复boot获取我写的系列教程。</p>
<p>官方网站：</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://undertow.io/">https://undertow.io/</a></p>
</blockquote>
<p>源码托管在 Github：</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://github.com/undertow-io/undertow">https://github.com/undertow-io/undertow</a></p>
</blockquote>
<h2 id="Undertow-有啥特性？"><a href="#Undertow-有啥特性？" class="headerlink" title="Undertow 有啥特性？"></a>Undertow 有啥特性？</h2><p><strong>1）HTTP/2 Support</strong></p>
<p>Undertow 支持 HTTP/2 开箱即用，不需要重写引导类路径。</p>
<p><strong>2）支持 HTTP 升级</strong></p>
<p>支持 HTTP 升级，允许多个协议通过 HTTP 端口上进行复用。</p>
<p><strong>3）支持 Web Socket</strong></p>
<p>Undertow 提供对 Web 套接字的全面支持，包括对 JSR-356 的支持。</p>
<p><strong>4）支持 Servlet 4.0</strong></p>
<p>Undertow 提供了对 Servlet 4.0 的支持，包括对嵌入式 Servlet 的支持，还可以混合部署 Servlet 和原生 Undertow 非阻塞处理程序。</p>
<p><strong>5）可嵌入式</strong></p>
<p>Undertow 可以嵌入到应用程序中，也可以通过几行代码独立运行。</p>
<p><strong>6）高灵活性</strong></p>
<p>一个 Undertow 服务器是通过链式处理器来配置的，可以根据需要添加功能，因此可以避免添加没有必要的功能。</p>
<h2 id="Undertow-性能如何？"><a href="#Undertow-性能如何？" class="headerlink" title="Undertow 性能如何？"></a>Undertow 性能如何？</h2><p>国外有篇帖子做了 Tomcat vs. Jetty vs. Undertow 三者的性能比较：</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://examples.javacodegeeks.com/enterprise-java/spring/tomcat-vs-jetty-vs-undertow-comparison-of-spring-boot-embedded-servlet-containers/">https://examples.javacodegeeks.com/enterprise-java/spring/tomcat-vs-jetty-vs-undertow-comparison-of-spring-boot-embedded-servlet-containers/</a></p>
</blockquote>
<p><img src="/2022/3FH16VE/1218593-20200824105306098-1526118676.png" alt="img"></p>
<p>从测试结果看，这三个 Servlet 容器都具有不错的性能，但 Undertow 性能更好，Tomcat 和 Jetty 紧随其后。</p>
<p>Jetty 在启动时的内存占用最大，为：311 MB, Tomcat 和 Undertow 的初始内存占用都很低，大约为：120 MB，而 Undertow 的初始内存占用最低，为：114 MB。</p>
<p>最后，关键的区别在于，Undertow 响应头参数默认包含 HTTP 持久连接信息，这个头参数在支持持久连接的客户端时，可以通过重用连接来优化性能。</p>
<h2 id="Show-me-the-code"><a href="#Show-me-the-code" class="headerlink" title="Show me the code"></a>Show me the code</h2><p>Undertow 目前有两个主要版本：</p>
<ul>
<li>2.1：当前支持 Servlet 4.0, JDK8+ 的稳定版本；</li>
<li>1.4：当前支持 Servlet 3.1, JDK7 的稳定版本；</li>
</ul>
<p>独立使用 Undertow 需要添加以下依赖：</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>io.undertow<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>undertow-core<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.1.0.Final<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>io.undertow<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>undertow-servlet<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.1.0.Final<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>io.undertow<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>undertow-websockets-jsr<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.1.0.Final<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>以下示例来源官网：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">HelloWorldServer</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(<span class="keyword">final</span> String[] args)</span> </span>&#123;</span><br><span class="line">        Undertow server = Undertow.builder()</span><br><span class="line">                .addHttpListener(<span class="number">8080</span>, <span class="string">&quot;localhost&quot;</span>)</span><br><span class="line">                .setHandler(<span class="keyword">new</span> HttpHandler() &#123;</span><br><span class="line">                    <span class="meta">@Override</span></span><br><span class="line">                    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">handleRequest</span><span class="params">(<span class="keyword">final</span> HttpServerExchange exchange)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">                        exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, <span class="string">&quot;text/plain&quot;</span>);</span><br><span class="line">                        exchange.getResponseSender().send(<span class="string">&quot;Hello World&quot;</span>);</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;).build();</span><br><span class="line">            server.start();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这是一个使用了异常 IO 的简单 Hello World 示例。</p>
<h2 id="Spring-Boot-amp-Undertow"><a href="#Spring-Boot-amp-Undertow" class="headerlink" title="Spring Boot &amp; Undertow"></a>Spring Boot &amp; Undertow</h2><p>上面讲到，Undertow 是为嵌入式而生的 Web 容器，又是 Spring Boot 默认集成的容器之一，下面栈长带大家来看下如何在 Spring Boot 中使用 Undertow。</p>
<p>因为在 <code>spring-boot-starter-web</code> 启动器中，Tomcat 是 Spring Boot 默认的嵌入式容器，即：<code>spring-boot-starter-tomcat</code>。</p>
<p>Spring Boot 还提供了其他两个启动器以方便进行代替：</p>
<ul>
<li>spring-boot-starter-jetty</li>
<li>spring-boot-starter-undertow</li>
</ul>
<p>下面来简单实战下，如何在 Spring Boot 中使用 Undertow。Spring Boot 基础知识就不介绍了，不熟悉的可以关注公众号Java技术栈在后台回复boot获取我写的系列教程。</p>
<p>排除 Tomcat 依赖，然后加入 Undertow 依赖：</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span><br><span class="line">    </span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-web<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">exclusions</span>&gt;</span></span><br><span class="line">            <span class="comment">&lt;!-- Exclude the Tomcat dependency --&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">exclusion</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-tomcat<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">exclusion</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">exclusions</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- Use Undertow instead --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-undertow<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>一步就完成集成了，当然实际情况还需要在 application 配置文件中加入 Undertow 的更多自定义或者优化配置参数。</p>
<p>Undertow容器的具体配置可以看这两个类：</p>
<ul>
<li>org.springframework.boot.autoconfigure.web.ServerProperties</li>
<li>org.springframework.boot.autoconfigure.web.ServerProperties.Undertow</li>
</ul>
<p>也可以看 Spring Boot 官方文档：</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://docs.spring.io/spring-boot/docs/2.3.2.RELEASE/reference/htmlsingle/#server-properties">https://docs.spring.io/spring-boot/docs/2.3.2.RELEASE/reference/htmlsingle/#server-properties</a></p>
</blockquote>
<p>上面有所有 Server 配置参数和说明。</p>
<p>再写一个测试方法测试下：</p>
<figure class="highlight typescript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> org.springframework.boot.SpringApplication;</span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.autoconfigure.SpringBootApplication;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.GetMapping;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RestController;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 微信公众号：Java技术栈</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@RestController</span></span><br><span class="line"><span class="meta">@SpringBootApplication</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Application</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="built_in">void</span> <span class="function"><span class="title">main</span>(<span class="params"><span class="built_in">String</span>[] args</span>)</span> &#123;</span><br><span class="line">        SpringApplication.run(Application.class);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@GetMapping</span>(value = <span class="string">&quot;/undertow/test&quot;</span>)</span><br><span class="line">    <span class="keyword">public</span> <span class="built_in">String</span> <span class="function"><span class="title">undertow</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;hello undertow&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>启动 Application：</p>
<p><img src="/2022/3FH16VE/1218593-20200824105306581-130064430.png" alt="img"></p>
<p>如上所示，可以看到 Undertow 的启动日志。</p>
<p>访问测试方法：</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="http://localhost:8080/undertow/test">http://localhost:8080/undertow/test</a></p>
</blockquote>
<p><img src="/2022/3FH16VE/1218593-20200824105306889-830058720.png" alt="img"></p>
<p>输出成功，测试完成。</p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>本文对 Undertow 作了一个介绍和集成实战，虽然 Undertow 性能很炸，但你可以去网上找一圈，根本没啥学习资料。</p>
<p>所以，对于学习和一般应用来说，Tomcat 足矣，一方面 Tomcat 学习资料多，另一方面 Tomcat 用的更广泛，很多坑别人帮你踩了，很多漏洞也已经暴露出来了。</p>
<p>那于那些一定要追求极致性能的又不想优化 Tomcat 的，可以考虑使用 Undertow，但同时你要有能力 Hold 住它，需要一定的积累经验，不然出一个问题你线上卡半天显然是不愿意看到的。</p>
<p>最后，网上很多文章说干掉 Tomcat 而要使用 Undertow 的，这就有点夸张，我只能呵呵了，持有保留意见，用啥都行，关键能不能用好。</p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/java/" rel="tag"># java</a>
              <a href="/tags/spring/" rel="tag"># spring</a>
              <a href="/tags/spring-boot/" rel="tag"># spring boot</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2022/1M55EP7.html" rel="prev" title="Netflix">
      <i class="fa fa-chevron-left"></i> Netflix
    </a></div>
      <div class="post-nav-item">
    <a href="/2022/7PXA8K.html" rel="next" title="Spring Cloud注册中心">
      Spring Cloud注册中心 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    
  <div class="comments">
    <div id="SOHUCS"></div>
  </div>
  

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%90%8A%E6%89%93-Tomcat-%EF%BC%8CUndertow-%E6%80%A7%E8%83%BD%E5%BE%88%E7%82%B8%EF%BC%81%EF%BC%81"><span class="nav-number">1.</span> <span class="nav-text">吊打 Tomcat ，Undertow 性能很炸！！ </span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#Undertow-%E4%BB%80%E4%B9%88%E9%AC%BC%EF%BC%9F"><span class="nav-number">1.1.</span> <span class="nav-text">Undertow 什么鬼？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Undertow-%E6%9C%89%E5%95%A5%E7%89%B9%E6%80%A7%EF%BC%9F"><span class="nav-number">1.2.</span> <span class="nav-text">Undertow 有啥特性？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Undertow-%E6%80%A7%E8%83%BD%E5%A6%82%E4%BD%95%EF%BC%9F"><span class="nav-number">1.3.</span> <span class="nav-text">Undertow 性能如何？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Show-me-the-code"><span class="nav-number">1.4.</span> <span class="nav-text">Show me the code</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Spring-Boot-amp-Undertow"><span class="nav-number">1.5.</span> <span class="nav-text">Spring Boot &amp; Undertow</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%80%BB%E7%BB%93"><span class="nav-number">1.6.</span> <span class="nav-text">总结</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">mixley</p>
  <div class="site-description" itemprop="description">全栈·架构设计·算法</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">43</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">19</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">10</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">mixley</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  

  <script>
  NexT.utils.loadComments(document.querySelector('#SOHUCS'), () => {
    var appid = 'cyvxnSprz';
    var conf = 'prod_45d8f2d8bd5b14b5c44442e45f977f85';
    var width = window.innerWidth || document.documentElement.clientWidth;
    if (width < 960) {
      window.document.write('<script id="changyan_mobile_js" charset="utf-8" type="text/javascript" src="https://changyan.sohu.com/upload/mobile/wap-js/changyan_mobile.js?client_id=' + appid + '&conf=' + conf + '"><\/script>');
    } else {
      var loadJs=function(d,a){var c=document.getElementsByTagName("head")[0]||document.head||document.documentElement;var b=document.createElement("script");b.setAttribute("type","text/javascript");b.setAttribute("charset","UTF-8");b.setAttribute("src",d);if(typeof a==="function"){if(window.attachEvent){b.onreadystatechange=function(){var e=b.readyState;if(e==="loaded"||e==="complete"){b.onreadystatechange=null;a()}}}else{b.onload=a}}c.appendChild(b)};loadJs("https://changyan.sohu.com/upload/changyan.js",function(){window.changyan.api.config({appid:appid,conf:conf})});
    }
  });
  </script>
  <script src="https://assets.changyan.sohu.com/upload/plugins/plugins.count.js"></script>

</body>
</html>
